//
// C++ Implementation: filterwidget
//
// Description:
//
//
// Author: Willi Ballenthin <wilbal1087@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "activityfilterwidget.h"

#include <QDebug>

ActivityFilterWidget::ActivityFilterWidget ( ActivityDbInterface* interface, QWidget *parent )
        : ActivityWidget ( interface,  parent )
{
    //TODO: remove filter group box. no need for this frame.
    ui.setupUi ( this );

    connect ( ui.pb_filter, SIGNAL ( clicked() ), this, SLOT ( applyFilter() ) );


    // hotwire in a typeselector instead of a combobox
    //ts_type = new TypeSelectorWidget(this);

    //ui.gb_type->layout()->addWidget(ts_type);
}

/**
* Determines the SQL fragment corresponding to the intended filter based on user input.
* @return A fragment of SQL syntax filtering the db results.
*/
// TODO: set the last filter in the config file so it gets loaded on next boot.
QString ActivityFilterWidget::getFilter()
{
    filter = "WHERE ";

    if ( ( ui.de_dateMin->date() != QDate ( 2000, 1, 1 ) ) )
    {
        QDate dateMin = ui.de_dateMin->date();
        filter += "date>'";
        filter += dateMin.toString("yyyyMMdd000000");
        filter += "' AND ";
    }

    if ( ( ui.de_dateMax->date() != QDate ( 2000, 1, 1 ) ) )
    {
        QDate dateMax = ui.de_dateMax->date();
        filter += " date<'";
        filter += dateMax.toString("yyyyMMdd000000");
        filter += "' AND ";
    }

    if ( ( ui.te_timeMin->time() != QTime ( 0, 0, 0 ) ) )
    {
        QTime timeMin = ui.te_timeMin->time();
        filter += "time>'";
        filter += timeMin.toString();
        filter += "' AND ";
    }

    if ( ( ui.te_timeMax->time() != QTime ( 0, 0, 0 ) ) )
    {
        QTime timeMax = ui.te_timeMax->time();
        filter += " time<'";
        filter += timeMax.toString();
        filter += "' AND ";
    }

    if ( ( ui.te_durationMin->time() != QTime ( 0, 0, 0 ) ) )
    {
        QTime durationMin = ui.te_durationMin->time();
        filter += "duration<'";
        filter += durationMin.toString();
        filter += "' AND ";
    }

    if ( ( ui.te_durationMax->time() != QTime ( 0, 0, 0 ) ) )
    {
        QTime durationMax = ui.te_durationMax->time();
        filter += "duration>'";
        filter += durationMax.toString();
        filter += "' AND ";
    }

    if ( ui.dsb_distanceMin->value() != 0 )
    {
        double distanceMin = ui.dsb_distanceMin->value();
        filter += "distance>";
        filter += QString::number ( distanceMin );
        filter += " AND ";
    }

    if ( ui.dsb_distanceMax->value() != 0 )
    {
        double distanceMax = ui.dsb_distanceMax->value();
        filter += " distance<";
        filter += QString::number ( distanceMax );
        filter += " AND ";
    }

    if ( ( ui.sb_effortMin->value() != 0 ) )
    {
        double effortMin = ui.sb_effortMin->value();
        filter += "effort<";
        filter += QString::number ( effortMin );
        filter += " AND ";
    }

    if ( ( ui.sb_effortMax->value() != 0 ) )
    {
        double effortMax = ui.sb_effortMax->value();
        filter += " effort>";
        filter += QString::number ( effortMax );
        filter += " AND ";
    }

    filter += this->getSelected();
    filter += " AND ";

    QString location = ui.le_location->text();
    if ( location.compare ( QString ( "location" ) ) != 0 &&
            location.compare ( QString ( "" ) ) != 0 )
    {
        filter += "location LIKE ";
        location.prepend ( "'%" );
        location.replace ( ", ", "%' AND location like '%" );
        location.replace ( ',', "%' AND location like '%" );
        location.append ( "%'" );
        filter += location;
        filter += " AND ";
    }

    QString route = ui.le_route->text();
    if ( route.compare ( QString ( "route" ) ) != 0 &&
            route.compare ( QString ( "" ) ) != 0 )
    {
        filter += "route LIKE ";
        route.prepend ( "'%" );
        route.replace ( ", ", "%' AND route like '%" );
        route.replace ( ',', "%' AND route like '%" );
        route.append ( "%'" );
        filter += route;
        filter += " AND ";
    }

    QString notes = ui.le_notes->text();
    if ( notes.compare ( QString ( "notes" ) ) != 0 &&
            notes.compare ( QString ( "" ) ) != 0 )
    {
        filter += "notes LIKE ";
        notes.prepend ( "'%" );
        notes.replace ( ", ", "%' AND notes like '%" );
        notes.replace ( ',', "%' AND notes like '%" );
        notes.append ( "%'" );
        filter += notes;
        filter += " AND ";
    }

    QString splits = ui.le_splits->text();
    if ( splits.compare ( QString ( "splits" ) ) != 0 &&
            splits.compare ( QString ( "" ) ) != 0 )
    {
        filter += "splits LIKE ";
        splits.prepend ( "'%" );
        splits.replace ( ", ", "%' AND splits like '%" );
        splits.replace ( ',', "%' AND splits like '%" );
        splits.append ( "%'" );
        filter += splits;
        filter += " AND ";
    }

    filter += " id>0 ";
    return filter;
}

QString ActivityFilterWidget::getSelected()
{
    if(ui.cb_all->isChecked())
    {
        return QString("TRUE");
    }

    QString fragment("(");

    if(ui.cb_run->isChecked())
    {
        QCheckBox* cur = ui.cb_run;
        if(fragment.length() > 1)
        {
            fragment += "OR type='";
            fragment += cur->text();
            fragment += "' ";
        }
        else
        {
            fragment += " type='";
            fragment += cur->text();
            fragment += "' ";
        }
    }

    if(ui.cb_workout->isChecked())
    {
        QCheckBox* cur = ui.cb_workout;
        if(fragment.length() > 1)
        {
            fragment += "OR type='";
            fragment += cur->text();
            fragment += "' ";
        }
        else
        {
            fragment += " type='";
            fragment += cur->text();
            fragment += "' ";
        }
    }

    if(ui.cb_race->isChecked())
    {
        QCheckBox* cur = ui.cb_race;
        if(fragment.length() > 1)
        {
            fragment += "OR type='";
            fragment += cur->text();
            fragment += "' ";
        }
        else
        {
            fragment += " type='";
            fragment += cur->text();
            fragment += "' ";
        }
    }

    if(ui.cb_lift->isChecked())
    {
        QCheckBox* cur = ui.cb_lift;
        if(fragment.length() > 1)
        {
            fragment += "OR type='";
            fragment += cur->text();
            fragment += "' ";
        }
        else
        {
            fragment += " type='";
            fragment += cur->text();
            fragment += "' ";
        }
    }

    if(ui.cb_core->isChecked())
    {
        QCheckBox* cur = ui.cb_core;
        if(fragment.length() > 1)
        {
            fragment += "OR type='";
            fragment += cur->text();
            fragment += "' ";
        }
        else
        {
            fragment += " type='";
            fragment += cur->text();
            fragment += "' ";
        }
    }

    if(ui.cb_walk->isChecked())
    {
        QCheckBox* cur = ui.cb_walk;
        if(fragment.length() > 1)
        {
            fragment += "OR type='";
            fragment += cur->text();
            fragment += "' ";
        }
        else
        {
            fragment += " type='";
            fragment += cur->text();
            fragment += "' ";
        }
    }

    if(ui.cb_other->isChecked())
    {
        QCheckBox* cur = ui.cb_other;
        if(fragment.length() > 1)
        {
            fragment += "OR type='";
            fragment += cur->text();
            fragment += "' ";
        }
        else
        {
            fragment += " type='";
            fragment += cur->text();
            fragment += "' ";
        }
    }

    if(ui.cb_bike->isChecked())
    {
        QCheckBox* cur = ui.cb_bike;
        if(fragment.length() > 1)
        {
            fragment += "OR type='";
            fragment += cur->text();
            fragment += "' ";
        }
        else
        {
            fragment += " type='";
            fragment += cur->text();
            fragment += "' ";
        }
    }

    if(ui.cb_cycle->isChecked())
    {
        QCheckBox* cur = ui.cb_cycle;
        if(fragment.length() > 1)
        {
            fragment += "OR type='";
            fragment += cur->text();
            fragment += "' ";
        }
        else
        {
            fragment += " type='";
            fragment += cur->text();
            fragment += "' ";
        }
    }

    if(ui.cb_swim->isChecked())
    {
        QCheckBox* cur = ui.cb_swim;
        if(fragment.length() > 1)
        {
            fragment += "OR type='";
            fragment += cur->text();
            fragment += "' ";
        }
        else
        {
            fragment += " type='";
            fragment += cur->text();
            fragment += "' ";
        }
    }
    
    fragment += ") ";

    qDebug() << "returning";

    return fragment;
}

/**
* Applies the user input and determines the current filter, and queries the database
* with this filter.
*/
void ActivityFilterWidget::applyFilter()
{
    getFilter();
    interface->setFilter ( filter );
    qDebug() << "Filtering: " << filter;
    interface->query();
}

/**
* Update the widget's UI.
*/
void ActivityFilterWidget::updateUi()
{
    // nothing to do here at this point.
}

